import { parseJsonStr, stringifyObj, getPermissions, getMenus } from '@common/utility'
export default {
  namespaced: true,
  state: () => ({
    isAgainLogin: true,
    vipDays: 30,
    permissions: null,
    userPermissions: null,
    userInfo: null,
    isOtherRole: null, // 不参与考核工作人员
    isNormalRole: null, // 普通工作人员
    isOrgRole: null, // 部门负责人
    isPartRole: null, // 分管负责人
    isMainRole: null, // 主要负责人
    isAdminRole: null, // 是否运营人员
    isApplyRole: null, // 是否物品领用审批人员
    isRepairRole: null, // 是否报修维修审批人员
    isReimburseRole: null, // 是否报销审批人员
    isBusinessRole: null, // 是否出差审批人员
    isWeekendRole: null, // 是否周末值班汇总人员
  }),
  getters: {
    vipDays: state => {
      if (state.userInfo) {
        return state.userInfo.vipDays || 0
      } else {
        const userInfo = localStorage.getItem('userInfo')
        if (userInfo) {
          state.userInfo = parseJsonStr(userInfo)
          return state.userInfo.vipDays || 0
        }
      }
      return 30
    },
    leftNavMenus: state => {
      const userPermissions = state.userPermissions ?? parseJsonStr(localStorage.getItem('PSKH_PERMISSIONS'))
      if (userPermissions) {
        if (state.leftMenuPcode) {
          const menu =
            findChildItem(userPermissions, state.leftMenuPcode, { fieldId: 'belongPage', fieldChild: 'menuVOS' }) || {}
          const { node } = menu
          if (node) {
            return node.menuVOS
          }
        }
      }
      return userPermissions
    },
    userInfo: state => {
      if (state.userInfo) {
        return state.userInfo
      } else {
        const storageUserInfo = localStorage.getItem('userInfo')
        if (storageUserInfo) {
          state.userInfo = parseJsonStr(storageUserInfo)
          return state.userInfo
        }
      }
      return null
    },
    isOtherRole: (state, getter) => {
      if (state.isOtherRole === null) {
        const userInfo = getter.userInfo
        state.isOtherRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_other_staff') !== undefined
      }
      return state.isOtherRole
    },
    isNormalRole: (state, getter) => {
      if (state.isNormalRole === null) {
        const userInfo = getter.userInfo
        state.isNormalRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_staff') !== undefined
      }
      return state.isNormalRole
    },
    isOrgRole: (state, getter) => {
      if (state.isOrgRole === null) {
        const userInfo = getter.userInfo
        state.isOrgRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_leader') !== undefined
      }
      return state.isOrgRole
    },
    isPartRole: (state, getter) => {
      if (state.isPartRole === null) {
        const userInfo = getter.userInfo
        state.isPartRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_part_leader') !== undefined
      }
      return state.isPartRole
    },
    isMainRole: (state, getter) => {
      if (state.isMainRole === null) {
        const userInfo = getter.userInfo
        state.isMainRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_main_leader') !== undefined
      }
      return state.isMainRole
    },
    isAdminRole: (state, getter) => {
      if (state.isAdminRole === null) {
        const userInfo = getter.userInfo
        state.isAdminRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_admin') !== undefined
      }
      return state.isAdminRole
    },
    isOperateRole: (state, getter) => {
      if (state.isOperateRole === null) {
        const userInfo = getter.userInfo
        state.isOperateRole = userInfo?.userId === 'admin'
      }
      return state.isOperateRole
    },
    isApplyRole: (state, getter) => {
      if (state.isApplyRole === null) {
        const userInfo = getter.userInfo
        state.isApplyRole =
          userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_item_apply_appr') !== undefined
      }
      return state.isApplyRole
    },
    isRepairRole: (state, getter) => {
      if (state.isRepairRole === null) {
        const userInfo = getter.userInfo
        state.isRepairRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_repair_appr') !== undefined
      }
      return state.isRepairRole
    },
    isReimburseRole: (state, getter) => {
      if (state.isReimburseRole === null) {
        const userInfo = getter.userInfo
        state.isReimburseRole =
          userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_reimburse_appr') !== undefined
      }
      return state.isReimburseRole
    },
    isBusinessRole: (state, getter) => {
      if (state.isBusinessRole === null) {
        const userInfo = getter.userInfo
        state.isBusinessRole =
          userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_business_trip_appr') !== undefined
      }
      return state.isBusinessRole
    },
    isWeekendRole: (state, getter) => {
      if (state.isWeekendRole === null) {
        const userInfo = getter.userInfo
        state.isWeekendRole =
          userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_weekend_duty') !== undefined
      }
      return state.isWeekendRole
    },
  },
  actions: {
    async getUserPermissions(context) {
      if (context.getters.userInfo) {
        const userPermissions = getMenus(context.getters)
        context.commit('setUserPermissions', userPermissions)
        context.commit('setPermissions', { list: userPermissions, homePage: true })
      }
    },
  },
  mutations: {
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo

      state.isOtherRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_other_staff') !== undefined

      state.isNormalRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_staff') !== undefined

      state.isOrgRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_leader') !== undefined

      state.isPartRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_part_leader') !== undefined

      state.isMainRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_main_leader') !== undefined

      state.isAdminRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_admin') !== undefined

      state.isOperateRole = userInfo?.userId === 'admin'

      state.isApplyRole =
        userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_item_apply_appr') !== undefined

      state.isRepairRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_repair_appr') !== undefined

      state.isReimburseRole =
        userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_reimburse_appr') !== undefined

      state.isBusinessRole =
        userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_business_trip_appr') !== undefined

      state.isWeekendRole = userInfo?.uiasRoleList.find(item => item.roleCode === 'identity_weekend_duty') !== undefined

      localStorage.setItem('userInfo', stringifyObj(userInfo))
    },
    // TODO 合并 多余的 permission
    setUserPermissions(state, val) {
      state.userPermissions = val
      localStorage.setItem('PSKH_PERMISSIONS', stringifyObj(val))
    },
    setPermissions(state, { list = [], homePage = false }) {
      let permissions = []
      if (list.length) {
        permissions = getPermissions(list, permissions)
      }
      if (homePage) {
        permissions.push('Home')
      }
      localStorage.setItem('PERMISSION_CODES', stringifyObj(permissions))
      state.permissions = permissions
    },
    permissions: state => {
      const permissions = state.permissions ?? parseJsonStr(localStorage.getItem('PERMISSION_CODES'))
      if (permissions && permissions.length) {
        return permissions
      }
      return null
    },
  },
}
